{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md-master/css/editormd.preview.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 300px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 500px;
            margin-left: -1px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading "><i class="fa fa-book" aria-hidden="true">&nbsp;wiki文档</i>
                <div class="function">
                    <a type="button" href="{% url 'wiki_add' project_id=request.tracer.project.id %}"
                       class="btn btn-success btn-xs">
                        <i class="fa fa-plus" aria-hidden="true"></i> 新建
                    </a>
                    {% if wiki_obj %}
                        <a type="button"
                           href="{% url 'wiki_edit' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}"
                           class="btn btn-primary btn-xs">
                            <i class="fa fa-edit" aria-hidden="true"></i> 编辑
                        </a>
                        <a type="button"
                           href="{% url 'wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}"
                           class="btn btn-danger btn-xs">
                            <i class="fa fa-trash" aria-hidden="true"></i> 删除
                        </a>
                    {% endif %}
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">
                    </ul>
                </div>
                <div class="col-sm-9 content">
                    {# 文章详情页预览 判断后端传来的数据是否存在，根据结果显示不同页面#}
                    {% if wiki_obj %}
                        <div id="previewMarkdown">
                            <textarea>{{ wiki_obj.content }}</textarea>
                        </div>
                    {% else %}
                        <div style="text-align: center;margin-top: 50px">
                            <h4 style="font-weight: bold">《{{ request.tracer.project.name }}》 wiki文档库</h4>
                            <a href="{% url 'wiki_add' project_id=request.tracer.project.id %}">
                                <i class="fa fa-plus" aria-hidden="true"></i> 新建文档
                            </a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor.md-master/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/jquery.flowchart.min.js' %}"></script>
    <script>
        var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.tracer.project.id %}"
        // 进入界面变发送 ajax 请求，获取后端发来的目录
        $(function () {
            initCataLog();
            initPreivewMarkdown();
        });

        function initPreivewMarkdown() {
            editormd.markdownToHTML("previewMarkdown", {
                htmlDecode: "style,script,iframe",
            });
        }

        function initCataLog() {
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.tracer.project.id %}',
                type: 'get',
                dataType: 'JSON',
                success: function (data) {
                    if (data.status) {
                        // 循环获取数据
                        $.each(data.data_list, function (index, item) {
                            // 判断每条数据的 item.parent_id 是否存在，如果没有则是第一级目录,并创建li标签，添加至url中
                            var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                            var li = $('<li>').attr('id', 'id_' + item.id).append($('<a>').text(item.title).attr("href", href)).append($('<ul>'))
                            if (!item.parent_id) {
                                // 找到li的父标签url，将其添加进去
                                $('#catalog').append(li);
                            } else {
                                var href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                                // 如果有parent_id 则表示子文章，根据parent_id 确定父文章的标签id，在找到父标签的儿子的ul,将其添加进去
                                $('#id_' + item.parent_id).children('ul').append(li)
                            }
                        })
                    }
                }
            })
        }
    </script>
{% endblock %}
